import React, { Component } from 'react'
const MsgContext = React.createContext()

class Home extends Component {
  static contextType = MsgContext
  render() {
    return (
      <div>
        <h2>home---{this.context}</h2>
        <MsgContext.Consumer>
          {(data) => {
            return <span>{data}</span>
          }}
        </MsgContext.Consumer>
      </div>
    )
  }
}

class About extends Component {
  static contextType = MsgContext
  render() {
    return (
      <div>
        <h2>About---{this.context}</h2>
        <MsgContext.Consumer>
          {(data) => {
            return <span>{data}</span>
          }}
        </MsgContext.Consumer>
      </div>
    )
  }
}

export default class App extends Component {
  render() {
    return (
      <div>
        <h1>App</h1>
        <MsgContext.Provider value="app组件传递的数据">
          <Home />
          <About />
        </MsgContext.Provider>
      </div>
    )
  }
}
